/**
 * This file is part of iSecurePasswords.
 *
 *   iSecurePasswords is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   iSecurePasswords is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with iSecurePasswords.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Matthew MacGregor
 * Created: 2012
 * iSecurePasswords, copyright 2013 Sudolink, LLC
 * 
 */ 
package com.sudolink.isp.ui.locktimer;

import com.sudolink.isp.app.Application;
import javax.swing.JOptionPane;

/**
 * The UnlockFrame is part of the timed lock functionality. When the application
 * has been idle for several minutes, the password UI will be hidden (locked) and
 * this UI will be shown instead.
 * 
 * @author Matthew MacGregor
 */
public class UnlockFrame extends javax.swing.JFrame {

    private final UnlockFrameManager manager;
    private final IManageLocks lockManager;
    
    /**
     * Creates new form UnlockFrame
     * @param lockManager
     */
    public UnlockFrame(IManageLocks lockManager) {
        initComponents();
        this.lockManager = lockManager;
        String savedUnlockCode = Application.getUnlockCodeMD5();
        System.out.println("Saved unlock code:" + savedUnlockCode);
        if( savedUnlockCode == null || savedUnlockCode.isEmpty() ) {
            pwKeypass.setText("Passcode Not Set");
            pwKeypass.setEnabled(false);
            pwKeypass.setEchoChar((char)0);
        }
        getRootPane().setDefaultButton(btnUnlock);
        manager = new UnlockFrameManager();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelMain = new javax.swing.JPanel();
        lblMessage = new javax.swing.JLabel();
        pwKeypass = new javax.swing.JPasswordField();
        btnUnlock = new javax.swing.JButton();
        lblKeyIcon = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("This Application Has Been Locked");
        setBounds(new java.awt.Rectangle(0, 2, 0, 0));
        setResizable(false);

        lblMessage.setText("<html>Enter your <b>passkey</b> to unlock the application. If you cannot remember your passkey, you may restart the application.</html>");
        lblMessage.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        btnUnlock.setText("Unlock");
        btnUnlock.setSelected(true);
        btnUnlock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUnlockActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout panelMainLayout = new org.jdesktop.layout.GroupLayout(panelMain);
        panelMain.setLayout(panelMainLayout);
        panelMainLayout.setHorizontalGroup(
            panelMainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelMainLayout.createSequentialGroup()
                .addContainerGap()
                .add(panelMainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(lblMessage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .add(panelMainLayout.createSequentialGroup()
                        .add(pwKeypass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 194, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnUnlock, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 88, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 0, Short.MAX_VALUE))))
        );
        panelMainLayout.setVerticalGroup(
            panelMainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelMainLayout.createSequentialGroup()
                .addContainerGap()
                .add(lblMessage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 26, Short.MAX_VALUE)
                .add(panelMainLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(pwKeypass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnUnlock))
                .addContainerGap())
        );

        lblKeyIcon.setBackground(new java.awt.Color(153, 153, 153));
        lblKeyIcon.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblKeyIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/sudolink/isp/media/password-icon-mid.png"))); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap(24, Short.MAX_VALUE)
                .add(lblKeyIcon, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 112, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panelMain, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(lblKeyIcon, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(panelMain, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnUnlockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUnlockActionPerformed

        boolean isUnlockedSuccessful = manager.Unlock(pwKeypass.getPassword());
        if( isUnlockedSuccessful ) {
            this.dispose();
            lockManager.unlockAll();
        } else {
            JOptionPane.showMessageDialog(this, "Sorry, that unlock code isn't correct.");
        }
        
    }//GEN-LAST:event_btnUnlockActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnUnlock;
    private javax.swing.JLabel lblKeyIcon;
    private javax.swing.JLabel lblMessage;
    private javax.swing.JPanel panelMain;
    private javax.swing.JPasswordField pwKeypass;
    // End of variables declaration//GEN-END:variables
}
